package com.example.demo1.controller;

import com.example.demo1.domain.Student;
import com.example.demo1.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class StudentController {

    @Autowired
    StudentService studentService;

    /**
     * 查询全部学生
     * @param model
     * @return
     */
    @RequestMapping("/student/list")
    public String list(Model model) {

        List<Student> students =studentService.findAll();
        model.addAttribute("data",students);

        return "student/list";
    }

    /**
     * 新增一位学生
     * @param student
     * @return
     */
    @PostMapping("/student/insert")
    public Student insert(Student student){
        Student student1 = studentService.insert(student);
        return student1;
    }

    /**
     * 更新一位学生信息
     * @param student
     * @return
     */
    @PutMapping("/student/update")
    public Student update(Student student) {
        Student student1 = studentService.update(student);
        return student1;
    }

    @DeleteMapping("/deleteStudent/{id}")
    public void deleteById(@PathVariable Integer id){
        studentService.deleteById(id);
    }

}
